<?php
    #-------------------------------------------------------------#
	# This file is part of the Kumomado - Virtual Window Platform #
	# for PHP. (c) 2009 Tsai, Min Che <tirme0812@gmail.com>       #
	# Please visit http://www.kumomado.org/ for more information. #
	#                                                             #
	# This software is licensed under the GNU License v3. Please  #
	# view the LICENSE file for the full text of this license.    #
    #-------------------------------------------------------------#

	Kumomado::load('MVCHelper.KumomadoView');
	/**
	 *
	 * @package libraries/mvchelper/view/
	 * @author Tsai, Min Che
	 * @version 1.0
	 * @abstract
	 */
	abstract class KumomadoAbstractView implements KumomadoView {
		/**
		 * @var mixed
		 */
		private $response = null;
		/**
		 * @var mixed
		 */
		private $assign_vars = null;
		/**
		 *
		 * @var string
		 */
		protected $layout;
		/**
		 * @param mixed $key
		 * @param mixed $value
		 */
		public function response($key, $value = null) {
			if (is_array($key)) {
				$this->response = array_merge((array)$this->response, $key);
			} else if (is_string($key) && $value == null) {
				$this->response[$key] = '';
			} else {
				if (isset($this->response[$key]) && is_array($this->response[$key])) {
					$this->response = array_merge_recursive($this->response, array($key => $value));
				} else {
					$this->response[$key] = $value;
				}
			}
		}
		public function assign($key, $value = null) {
			$this->assign_vars[$key] = $value;
		}
		protected function get($key = '') {
			if (isset($this->response[$key])) {
				return $this->response[$key];
			}
			return $this->response;
		}
		abstract public function layout($xsl_file);
		protected function output($data_type = 'text') {
			switch ($data_type) {
				case 'none':
					return $this->response;
					break;
				case 'text':
				case 'html':
					$processor = new KumomadoXsltProcessor();
					$processor->importArray($this->response);
					$processor->importStyleSheet($this->layout);
					echo $processor->transformToDoc();
					break;
				case 'xml':
					$xml_writer = new KumomadoXmlWriter('Result');
					$xml_writer->fromArray($this->response);
					$xml_writer->output();
					break;
				case 'json':
					echo json_encode($this->response);
					break;
				default:
					if (is_string($this->response)) {
						echo $this->response;
					} else if (is_array($this->response)) {
						echo json_encode($this->response);
					}
					break;
			}
		}
		public function __get($key) {
			return isset($this->assign_vars[$key]) ? $this->assign_vars[$key] : null;
		}
		public function __set($key, $value) {
			$this->assign($key, $value);
		}
	}
?>